﻿using System;
using System.Collections.Generic;

using CommunityServer.Components;
using CommunityServer.Controls;

using CSLive.ControlPanel.Editor;

namespace CSLive.Controls
{
    public class UserLocationMappingSubForm : WrappedSubFormBase, ICSLiveControl
    {
        // storage for the reference to the DropDownList containing car brands
        private YesNoCheckBox YesNo;

        #region Methods

        public override bool IsEnabled()
        {
            return true;
        }

        protected override void AttachChildControls()
        {
            // find the DropDownList that will contain car brands
            YesNo = CSControlUtility.Instance().FindControl( this, YesNoId ) as YesNoCheckBox;

            // if the DropDownList couldn't be found, throw an exception
            if ( YesNo == null )
                throw new InvalidOperationException( "The YesNoId must identify a valid YesNoCheckBox to render a PresenceSubForm" );
        }

        public override void DataBind()
        {
            base.DataBind();

            var user = DataSource as User;
            if ( user != null && !Page.IsPostBack )
                YesNo.Checked = !string.IsNullOrEmpty( user.GetExtendedAttribute( "EnableLocationMapping" ) );
        }

        public override void ApplyChangesBeforeCommit( object activeObject )
        {
            var user = activeObject as User;
            if ( YesNo.Checked && user != null )
                user.SetExtendedAttribute( "EnableLocationMapping", "true" );
            else if ( user != null ) user.SetExtendedAttribute( "LivePresence", "" );
        }

        #endregion

        #region Properties

        // this property will be used to allow the theme developer to provide the ID of the DropDownList containing car brands
        public string YesNoId
        {
            get { return ( string ) ( ViewState[ "YesNoId" ] ?? "" ); }
            set { ViewState[ "YesNoId" ] = value; }
        }

        public Dictionary< string, Control > EditorControls
        {
            get
            {
                Dictionary< string, Control > dictionary;
                dictionary = new Dictionary< string, Control >
                                 {
                                     {
                                         "Sub Form", new Control( "Sub Form",
                                                                  "The User Location Mapping Sub Form",
                                                                  false,
                                                                  "<CSLive:UserLocationMappingSubForm runat=\"server\" YesNoId=\"usermappingyesno\" id=\"UserLocationMapping\"><FormTemplate><tr><td class=\"CommonFormFieldName\">Enable Location Maps</td><td class=\"CommonFormField\"><CSControl:YesNoCheckBox runat=\"server\" ID=\"usermappingyesno\" /></td></tr></FormTemplate></CSLive:UserLocationMappingSubForm>" )
                                         }
                                 };
                return dictionary;
            }
        }

        public string Name
        {
            get { return "User Location Mapping Sub Form"; }
        }

        public Uri DocsUri
        {
            get { return new Uri( "http://communityserverlive.com/Controls/User-Location-Mapping-Sub-Form/default.aspx" ); }
        }

        public string Description
        {
            get { return "Enable the Mapping of the User's Location onto a Sub Form"; }
        }

        public string Group
        {
            get { return "Live Maps"; }
        }

        #endregion
    }
}